﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using ControleRepresentante.Logic.Models;
using WebSite.ViewModels.Fields;

namespace WebSite.ViewModels.IndexViewModels
{
    public static class ClienteIndexColumns
    {
        public static Guid RazaoSocial = new Guid("f752ed79-caab-4a82-8b56-1d47de359833");
        public static Guid NomeFantasia = new Guid("9b80abfc-f25d-48fc-98e2-2af2c440b66c");
        public static Guid Vendedor = new Guid("b009acaa-9672-49ae-9f28-11baf42223a4");
        public static Guid CNPJ = new Guid("1009acaa-9672-49ae-9f28-11baf42223a4");
        public static Guid InscricaoEstadual = new Guid("2009acaa-9672-49ae-9f28-11baf42223a4");
        public static Guid ClienteDesde = new Guid("3009acaa-9672-49ae-9f28-11baf42223a4");
        public static Guid UltimoPedido = new Guid("94684387-8490-4D9D-8A42-CD9B3FB25724");
    }


    public class ClienteIndexViewModel : UsuariosHolderIndexViewModel<Cliente>
    {
        private readonly bool m_CanViewVendedor;
        public Dictionary<Guid, string> VendedorBusca { get; set; }
        public string NomeBuscaValue { get; set; }
        public string VendedoresBuscaValue { get; set; }
        public DateTime? ClienteDesdeAteBusca { get; set; }
        public DateTime? ClienteDesdeDeBusca { get; set; }
        public DateTime? NoPedidosDeBusca { get; set; }
        public DateTime? NoPedidosAteBusca { get; set; }

        public ClienteIndexViewModel(bool canViewVendedor, bool canCreatePedido)
            : base("Clientes", canCreatePedido)
        {
            m_CanViewVendedor = canViewVendedor;
        }

        protected override void RegisterColumns(IndexColumnDefinitionsCollectionWrapper<Cliente> indexColumnDefinitionsCollectionWrapper)
        {
            indexColumnDefinitionsCollectionWrapper.Register(ClienteIndexColumns.RazaoSocial, (cliente, htmlHelper) => htmlHelper.ActionLink(cliente.RazaoSocial, "Details", new { id = cliente.Id }));
            indexColumnDefinitionsCollectionWrapper.Register(ClienteIndexColumns.NomeFantasia, x => x.NomeFantasia);
            Func<Cliente, HtmlHelper, object> vendedorTemplate;
            if (m_CanViewVendedor)
            {
                vendedorTemplate = (cliente, htmlHelper) =>
                {
                    if (cliente.Vendedor != null)
                    {
                        return htmlHelper.ActionLink(cliente.VendedorNome, "Details", "Vendedor", new {id = cliente.Vendedor.Id}, null);
                    }
                    return String.Empty;
                };
            }
            else
            {
                vendedorTemplate = (cliente, htmlHelper) => cliente.VendedorNome;
            }
            indexColumnDefinitionsCollectionWrapper.Register(ClienteIndexColumns.Vendedor, vendedorTemplate);
            indexColumnDefinitionsCollectionWrapper.Register(ClienteIndexColumns.CNPJ, x => x.CNPJ);
            indexColumnDefinitionsCollectionWrapper.Register(ClienteIndexColumns.InscricaoEstadual, x => x.InscricaoEstadual);
            indexColumnDefinitionsCollectionWrapper.Register(ClienteIndexColumns.ClienteDesde, x => x.ClienteDesde);
            indexColumnDefinitionsCollectionWrapper.Register(ClienteIndexColumns.UltimoPedido, x => x.LastPedido);
            base.RegisterColumns(indexColumnDefinitionsCollectionWrapper);
        }

        protected override IEnumerable<IField> GetSearchFields()
        {
            List<IField> result = new List<IField>
            {
                new AutoCompleteField("Nome", "nome", NomeBuscaValue, "SearchCliente", "Search"),
                new TokenInputField("Vendedores", "vendedorIds", VendedoresBuscaValue, "Search", "SearchVendedor", VendedorBusca),
                new DatePickerField("Cliente Desde De", "clienteDesdeDe", ClienteDesdeDeBusca),
                new DatePickerField("Cliente Desde Até", "clienteDesdeAte", ClienteDesdeAteBusca),
                new DatePickerField("Sem Pedidos De", "noPedidosDe", NoPedidosDeBusca),
                new DatePickerField("Sem Pedidos Até", "noPedidosAte", NoPedidosAteBusca),
            };
            result.AddRange(base.GetSearchFields());
            return result;
        }

        protected override IEnumerable<ButtonType> ButtonTypes(Cliente modelBase)
        {
            return base.ButtonTypes(modelBase).Union(new [] {ButtonType.Print});
        }
    }
}